<?php

namespace app\admin\model;

use app\admin\traits\Result;
use think\facade\Request;
use think\Model;

class Article extends Model
{
    //
    public function edit($data){
        //验证数据

        $validate = validate('article');

        if (!$validate->scene('edit')->check($data)){
            $validate->getError() != '验证令牌无效' ? $token = Request::token() : $token = '';
            $msg = Result::error($validate->getError(),null,['token'=>$token]);
            return $msg;
        }
        $result = self::save($data,['id'=>$data['id']]);
        if ($result !== false){
            $msg = Result::success('编辑轮播图成功',url('/admin/articleList'));
        }else{
            $msg = Result::error('编辑轮播图失败');
        }
        return $msg;
    }
    public function add($data){
        $validate = validate('article');
        if (!$validate->scene('add')->check($data)){
            $validate->getError() != '验证令牌无效' ? $token = Request::token() : $token = '';
            $msg = Result::error($validate->getError(),null,['token'=>$token]);
            return $msg;
        }
        $result = self::save($data);
        if ($result !== false){
            $msg = Result::success('添加轮播图成功',url('/admin/articleList'));
        }else{
            $msg = Result::error('添加轮播图失败');
        }
        return $msg;
    }

    public function searchNameAttr($query,$value,$data){
        return $query->where('name','like','%'.$value.'%');
    }
}
